-
Notifications
You must be signed in to change notification settings - Fork 213
allow passing default values to environment plugin #657
Conversation
packages/env/index.js
Outdated
.use(EnvironmentPlugin, ['NODE_ENV', ...envs]); | ||
.use(EnvironmentPlugin, Array.isArray(envs) ? | ||
['NODE_ENV', ...envs] : | ||
[{ NODE_ENV: 'development', ...envs }]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the envs
value is an array, we should be spreading into the array instead of the object:
[{ NODE_ENV: 'development' }, ...envs]);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, we should probably be using the value from neutrino.options.env.NODE_ENV
.
[{ NODE_ENV: neutrino.options.env.NODE_ENV }, ...envs]);
Thoughts? cc: @SeeThruHead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late reply, crazy week.
Not sure about spreading into the array
[{ NODE_ENV: 'development' }, ...envs])
because according to the Webpack docs, it can be an array OR an object (which then holds the default values).
There's also a syntax problem with older node versions which don't allow spreading object anyways.
I will make the neutrino.options.env.NODE_ENV
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Older Node doesn't allow spreading into objects, but does allow arrays, at least for the Node versions that Neutrino supports.
Doesn't an array of objects support both, by allowing multiple values with defaults?
[
// Pulls in NODE_ENV with the value defaulted to neutrino.options.env.NODE_ENV
{ NODE_ENV: neutrino.options.env.NODE_ENV },
// Brings in any other envs, whether they are strings or objects
...envs
];
Looks like we had a merge in master here instead of a rebase, so it's showing a lot more commits. Could you fix that? Thanks! |
I had another read of https://webpack.js.org/plugins/environment-plugin/. It either takes an array of string keys, or an object mapping the keys to the default values. Unfortunately the Right now the only thing we can do is leave the plugin as is, and allow consumers to override it if they want to replace the arg. We will have to wait on this PR until we are ready for Neutrino v9. Then we can switch to module.exports = (neutrino, opts = {}) => {
neutrino.config
.plugin('env')
.use(EnvironmentPlugin, merge({
NODE_ENV: 'development'
}, opts);
}; Unfortunately right now we can't move forward. 😢 |
I'm a patient guy and for now I'll use my patched branch :)
…On Tue, Jan 16, 2018 at 2:52 PM, Eli Perelman ***@***.***> wrote:
I had another read of https://webpack.js.org/plugins/environment-plugin/.
It either takes an array of string keys, or an object mapping the keys to
the default values.
Unfortunately the else clause will violate that. I think the correct
course of action to take would be to switch this middleware to always take
an object; the bad news: that's a breaking change.
Right now the only thing we can do is leave the plugin as is, and allow
consumers to override it if they want to replace the arg. We will have to
wait on this PR until we are ready for Neutrino v9. Then we can switch to
module.exports = (neutrino, opts = {}) => {
neutrino.config
.plugin('env')
.use(EnvironmentPlugin, merge({
NODE_ENV: 'development'
}, opts);
};
Unfortunately right now we can't move forward. 😢
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#657 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAEBsxDTGCLrlVX6rgnWHf4_JF8WI0cSks5tLKmugaJpZM4RVaoX>
.
--
¦ Valentin Vago
¦ valentin.vago@gmail.com
¦ http://irata.graphics
|
@zeropaper with master now representing what will be released in v9, would you be willing to pick this up again? |
Shouldn't this line be backwards compatible and allow passing an object? .use(EnvironmentPlugin, Array.isArray(envs) ? ['NODE_ENV', ...envs] : { 'NODE_ENV': 'development', ...envs }); (see #749) |
pluginOptions = ['NODE_ENV', ...envs]; | ||
} | ||
else { | ||
pluginOptions = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ftes i think it should be backward compatible (and my PR reflects it: 4f6b4bf).
Your PR is slightly different because it wraps the object inside an array.
I think this is what @eliperelman meant with
Unfortunately the else clause will violate that.
Not urgent for me, happy to wait. Let me know if I can be of any more help. |
I actually dug a bit and apparently, the use(plugin, args = []) {
return this
.set('plugin', plugin)
.set('args', args);
} the instanciation of the plugin is done in this.init((Plugin, args = []) => new Plugin(...args)); // it hardly would make sense to me for "args" to be an object so, unless I miss something.. this PR should be merged as-is. |
So there are a few things here that I think made this issue confusing:
As such, the current Now that I'll open a PR for this now, since removing |
Since `EnvironmentPlugin` doesn't require any non-webpack dependencies or complex configuration, it's simpler to inline the plugin directly in presets that use it (which is currently just the web preset). The argument passed to the plugin is now correctly wrapped in an array (as expected by `webpack-chain`), meaning the object form that allows setting defaults is now also supported: https://webpack.js.org/plugins/environment-plugin/#usage-with-default-values Closes #657.
as described here:
https://webpack.js.org/plugins/environment-plugin/#usage-with-default-values